package com.spider.moudle.entity;

import org.apache.http.Header;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 夏茂轩
 * @description Cookie类
 * @date 2017/11/8 11:19
 */
public class Cookie {
    private final String name;
    private final String value;
    private final String path;
    private final String domain;
    private final Date expiry;

    public Cookie(String name, String value, String path, String domain, Date expiry) {
        this.name = name;
        this.value = value;
        this.path = path;
        this.domain = domain;
        this.expiry = expiry;
    }

    public Cookie(String name, String value, String path, String domain) {
        this.name = name;
        this.value = value;
        this.path = path;
        this.domain = domain;
        this.expiry = new Date();
    }

    public Cookie(String name, String value) {
        this.name = name;
        this.value = value;
        this.path = "/";
        this.domain = "";
        this.expiry = new Date();
    }

    public String getName() {
        return name;
    }

    public String getValue() {
        return value;
    }

    public String getPath() {
        return path;
    }

    public String getDomain() {
        return domain;
    }

    public Date getExpiry() {
        return expiry;
    }

    /**
     * @param [cookies] com.spider.moudle.entity.Cookie 的集合,用于cookie转换
     * @return java.lang.String
     * @author 夏茂轩
     * @description 静态方法
     * @date 2017/11/8 10:58
     */
    public static String toCookieString(Collection<Cookie> cookies) {
        String str = "";
        synchronized (cookies) {
            for (Cookie cookie : cookies) {
                str += cookie.getName() + "=" + cookie.getValue() + ";";
            }
            cookies.notifyAll();
        }
        return str;
    }

    /**
     * @param [cookieStr] cookie 字符串 eg:BIDUPSID=138391B2D20C45FC1E70717E0F53AEE1; PSTM=1505983957;
     * @return java.util.List<com.spider.moudle.entity.Cookie> 返回构建好的Cookie集合
     * @author 夏茂轩
     * @description 字符串转换为Cookie
     * @date 2017/11/8 11:13
     */
    public static List<Cookie> tranlateToCookie(String cookieStr) throws Exception {
        String[] rs = cookieStr.split(";");
        List<Cookie> aList = new ArrayList<>();
        for (String ss : rs) {
            if (ss != null && !ss.isEmpty()) {
                String[] tt = ss.split("=");
                Cookie cookie = new Cookie(tt[0], tt[1]);
                aList.add(cookie);
            }
        }
        return aList;
    }

    /**
     * @param [cookies] org.openqa.selenium.Cookie 需要转换的cookie
     * @return java.util.List<com.spider.moudle.entity.Cookie> 转换好的Cookie
     * @author 夏茂轩
     * @description 转换工具
     * @date 2017/11/8 11:32
     */
    public static ArrayList<Cookie> tranlateToCookie(Collection<org.openqa.selenium.Cookie> cookies) throws Exception {
        ArrayList<Cookie> cs = new ArrayList<>();
        synchronized (cookies) {
            cs = cookies.stream().map(cookie -> {
                return new Cookie(cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getExpiry());
            }).collect(Collectors.toCollection(ArrayList::new));
            cookies.notifyAll();
        }
        return cs;
    }

    public static ArrayList<Cookie> headersTranlateToCookie(Collection<Header> headers) throws Exception {
        ArrayList<Cookie> cookies = new ArrayList<>();
        for (Header header : headers) {
            if (header.getName().equals("Set-Cookie")) {
                try {
                    String val = header.getValue();
                    String[] array = val.split(";");
                    String[] cookArr = array[0].split("=");
                    Cookie cookie = new Cookie(cookArr[0], cookArr[1]);
                    cookies.add(cookie);
                } catch (Exception e) {
                    throw e;
                }
            }
        }
        return cookies;
    }
}
